class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int left = 0;
        int right = 0;
        int num = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            num += nums[i];
        }
        int sum = num - x;
        int s = 0;
        int count = -1;
        while (left < nums.size() && right < nums.size())
        {
            s += nums[right];
            while (s > sum && left < nums.size())
            {
                s -= nums[left++];
            }
            if (s == sum)
            {
                count = max(count, right - left + 1);
            }
            right++;
        }
        if (count == -1) return -1;
        return nums.size() - count;
    }
};